#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define N 10
double average(double *x) //平均值
{
	int i, j;
	double av, y[N];
	av = 0;
	for (i = 0; i < N; i++)
		av += x[i] / N;
	return av;
}
double max(double x[], int len) //最大值
{
	int i;
	double max = x[0];
	for (i = 0; i < len; i++)
	{
		if (x[i] > max)
			max = x[i];
	}
	return max;
}
double min(double x[], int len) //最小值
{
	int i;
	double min = x[0];
	for (i = 0; i < len; i++)
	{
		if (x[i] < min)
			min = x[i];
	}
	return min;
}

void sort(double x[]) //冒泡排序法
{
	int i, j;
	double temp;
	for (i = 1; i < N; i++)
	{
		for (j = 1; j < N; j++)
		{
			if (x[j] < x[j - 1])
			{
				temp = x[j];
				x[j] = x[j - 1];
				x[j - 1] = temp;
			}
		}
	}
}
void printarr(double x[N])
{
	int i;
	for (i = 0; i < N; i++)
	{
		printf("%10.3f", x[i]);
	}
}

int BinarySearchRecursive(double *array, int low, int high, int key)
{ //递归半折查找
	if (low > high)
		return -1;
	int mid = (low + high) / 2;

	if (array[mid] == key)
		return mid;
	else if (array[mid] < key)
		return BinarySearchRecursive(array, mid + 1, high, key);
	else
		return BinarySearchRecursive(array, low, mid - 1, key);
}
int main()
{
	int i, find, location;
	double x[N];
	system("cls");
	printf("\n随机生成数组：\n");

	srand(time(0));
	for (i = 0; i < N; i++) //数组输入
	{
		x[i] = rand() % 100 + 1 + (rand() % 100) * 0.01;
	}
	printarr(x);
	printf("\n数组排序后为:\n");
	sort(x);
	printarr(x);
	printf("\n数组的平均数是%.3f\n", average(x));
	printf("\n数组的最大值是:%.3f\n", max(x, N));
	printf("\n数组的最小值是:%.3f\n", min(x, N));
	printf("请输入要查找的数字:");
	scanf("%d", &find);
	location = BinarySearchRecursive(x, 0, N, find);
	if (location != -1)
	{
		printf("查找的字符在：%d", location + 1);
	}
	else
	{
		printf("没有找到\n");
	}
	return 0;
}
